<template>
  <div class="comment-post">
    <van-field class="post-field" v-model="message" rows="2" autosize type="textarea" maxlength="50" placeholder="请输入留言"
      show-word-limit @keyup.13="onPost" />
    <van-button class="post-btn" :disabled='message.trim().length===0' @click='onPost'>发布
    </van-button>
  </div>
</template>

<script>
import { addComment } from '@/api/comment.js'
export default {
  name: 'CommentPost',
  components: {},
  props: {
    target: { type: [String, Number, Object], required: true },
    art_id: { type: [String, Number, Object], defalut: null }
  },
  data () {
    return {
      message: ''
    }
  },
  computed: {},
  watch: {},
  created () { },
  mounted () { },
  methods: {
    async onPost () {
      try {
        const { data } = await addComment({ target: this.target, content: this.message, art_id: this.art_id })
        this.$toast('发布文章成功！')
        this.message = ''
        this.$emit('addComment')
        data.data.new_obj.is_liking = false

        this.$emit('onList', data.data.new_obj)
      } catch (err) {
        this.$toast('发布文章失败！')
      }
    }
  }
}
</script>

<style scoped lang="less">
.comment-post {
  display: flex;
  align-items: center;
  padding: 32px 0 32px 32px;
  .post-field {
    background-color: #f5f7f9;
  }
  .post-btn {
    width: 150px;
    border: none;
    padding: 0;
    color: #6ba3d8;
    &::before {
      display: none;
    }
  }
}
</style>
